/**
 * Created by zengy on 2017/7/18.
 */
angular.module('mainApp.member', ['ui.router'])
    .config(function($stateProvider) {
        $stateProvider
            .state({//模板列表页面
                name : 'member/memberList',
                url:'/member/memberList',
                templateUrl:'views/member/member_list.html?v='+new Date().getMinutes(),
                controller:'memberCtrl'
            })
    })
    .controller('memberCtrl',function ($scope) {
        var date=new Date();
        $scope.$emit('arg',10)
        $scope.vipModel={
            pageNo:1,
            pageSize:10
        }
        $('.date-picker').datepicker({
            autoclose: true,
            changeMonth: true,
            language : 'zh-CN',
            format : 'yyyy-mm-dd',
        })
        $scope.pagination = function (currentPage, pageCount) {
            $('.page-function #pagination').remove();
            $('.page-function').html('<ul id="pagination" class="pagination"></ul>');
            $('.pagination').twbsPagination({
                totalPages: pageCount,
                startPage: currentPage,
                visiblePages: 5,
                first: "首页",
                prev: "上一页",
                next: "下一页",
                last: "尾页",
                href: 'javascript:;',
                //hideOnlyOnePage : true,
                onPageClick: function (event, page) {
                    $scope.getVipList(page)
                }
            });
        };
        $scope.getVipList=function (page) {
            if(page){
                $scope.vipModel.pageNo=page;
            }
            layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
            new HttpRequest().queryUserVipList($scope.vipModel,function (res) {
                layer.closeAll();
                if (res.code == '000') {
                    $scope.vipList = res.data.list;
                    $scope.$apply();
                    if ($scope.vipList.length > 0) {
                        $scope.pagination($scope.vipModel.pageNo, res.data.pageCount);
                    }
                } else {
                    layer.msg(res.msg)
                }
            })
        }
        $scope.getVipList($scope.vipModel.pageNo);
        //通话记录
        $scope.conversationList=function (event) {
            var _this=$(event.target);
            var dataDetail=_this.closest('.callPhone').data('detail');
            var orderNo=dataDetail.orderNo;
            new HttpRequest().queryMobileLog({orderNo:orderNo},function (res) {
                if(res.code =='000'){
                    var conList=res.data || [];
                    var html='';
                    conList.map(function (index) {
                        html+= index.createTime+'，'+index.callStaus+':'+index.callTime+'秒<br />';
                    });
                    if(html){
                        layer.open({
                            title:'通话记录',
                            type: 1,
                            area: ['420px', '240px'],
                            content: html
                        });
                    }else{
                        layer.msg('暂无通话记录')
                    }

                }else{
                    layer.msg('获取失败！')
                }
            })
        }

        //拨打患者电话
        $scope.callUser=function (event) {
            var _this=$(event.target);
            var dataDetail=_this.closest('.callPhone').data('detail');
            //$scope.dataDetail= dataDetail;
            layer.confirm('与'+dataDetail.userName+'联系<br />电话'+ dataDetail.cellPhone, {
                title:'拨打电话',
                btn: ['确认拨打','取消拨打'] //按钮
            }, function(){
                layer.closeAll();
                new HttpRequest().customServiceCalling({orderNo: dataDetail.orderNo,toMobile: dataDetail.cellPhone},function (res) {
                    if(res.code =!'000'){
                        layer.msg('通话失败！');
                    }else{
                        layer.msg('拨打中。。。')
                    }
                })
            }, function(){
                layer.closeAll();
            });
        }
        //客服拨打医生电话
        $scope.callDoctor=function () {
            var _this=$(event.target);
            var dataDetail=_this.closest('.callPhone').data('detail');
            layer.confirm('与'+dataDetail.dotorName+'联系<br />电话'+ dataDetail.doctorMobile, {
                title:'拨打电话',
                btn: ['确认拨打','取消拨打'] //按钮
            }, function(){
                layer.closeAll();
                new HttpRequest().customServiceCalling({orderNo: dataDetail.orderNo,toMobile: dataDetail.doctorMobile},function (res) {
                    if(res.code =!'000'){
                        layer.msg('通话失败！');
                    }else{
                        layer.msg('拨打中。。。')
                    }
                })
            }, function(){
                layer.closeAll();
            });


        }
        //双向拨打
        $scope.callDouble=function () {
            var _this=$(event.target);
            var dataDetail=_this.closest('.callPhone').data('detail');
            //$scope.dataDetail= dataDetail;
            layer.confirm('接通',{
                type: 1,
                title:'接通',
                area: ['300px', '210px'], //宽高
                content: '<div style="padding: 10px;">'+
                '<p>用户'+dataDetail.userName+'('+dataDetail.cellPhone+')</p>'+
                '<p>与</p>'+
                '<p>医生'+dataDetail.dotorName+'('+dataDetail.doctorMobile+')</p>'+
                '</div>',
                btn: ['确认拨打', '取消拨打']
            },function () {

                new HttpRequest().doubleTalk({orderNo:dataDetail.orderNo ,wxOpenId:'suite',userMobile:dataDetail.cellPhone,doctorMobile:dataDetail.doctorMobile},function (res) {
                    if(res.code !='200' && res.data !='200'){
                        layer.msg('双向通话失败！')
                    }
                })
            },function () {
                layer.closeAll();

            });
        }
        //重新预约
        $scope.retime={};
        $scope.reconversation=function(event){
            var _this=$(event.target);
            var dataDetail=_this.data('detail');
            var orderTime= + dataDetail.consultationTime
            if(orderTime){
                var year=new Date(orderTime).getFullYear();
                var month=new Date(orderTime).getMonth()+1;
                var day=new Date(orderTime).getDate();
                var hour=(new Date(orderTime).getHours() < 10 ? '0'+new Date(orderTime).getHours() : new Date(orderTime).getHours());
                var min=(new Date(orderTime).getMinutes() < 10 ? '0'+new Date(orderTime).getMinutes() : new Date(orderTime).getMinutes());
                if(parseInt(hour)<9){
                    $('select.hour').find("option[value='08']").prop("selected",true);
                }else{
                    $('select.hour').find("option[value='"+hour+"']").prop("selected",true);
                }
                $('select.sec').find("option[value='"+min+"']").prop("selected",true);
                $scope.orderTime=year+'-'+month+'-'+day;
            }else{
                $scope.orderTime=date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate();
            }
            $scope.retime={
                wxOpenId:dataDetail.wxOpenId,
                orderId:dataDetail.orderId,
                doctorUserId:dataDetail.doctorUserId,
                userName:dataDetail.userName,
                cellPhone:dataDetail.cellPhone
            }
        }
        $scope.commitReTime=function () {
            var time=$('input[name="date"]').val();
            var hour=$('select.hour').val();
            var sec=$('select.sec').val();
            var consultationTime=time+' '+hour+':'+sec+':00';
            if(new Date(consultationTime).getTime()< date.getTime()){
                layer.msg('预约时间不能小于当前时间！');
                return false;
            }
            $scope.retime.consultationTime=consultationTime;
            new HttpRequest().makeACall($scope.retime,function (res) {
                if(res.code !='000' && data !=='1'){
                    layer.msg('修改失败！');
                    layer.closeAll();
                }else{
                    location.reload();
                }
            })

        }

    })
    .filter('idcard', function() { //可以注入依赖
        return function(text) {
            return text.slice(0,14)+'*****';
        }
    });